/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：呼叫中心-数据源-数据持久层-任务调度
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taiping.cc.job.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.taiping.cc.application.constant.dao.ActiveEnum;
import com.taiping.cc.application.constant.dao.BatchSizeEnum;
import com.taiping.cc.application.constant.service.PlatformServiceEnum;
import com.taiping.cc.job.dao.mapper.HttpRequestConfigMapper;
import com.taiping.cc.job.dataobject.HttpRequestConfigDO;

import java.util.ArrayList;
import java.util.List;

import com.taipingframework.mybatis.CommonDAO;
import com.taipingframework.utility.exception.DaoException;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 请求逻辑配置表 服务实现类
 * </p>
 *
 * @author 周孝兵
 * @since 2021-11-29
 */
@Repository
public class HttpRequestConfigDAO extends CommonDAO<HttpRequestConfigMapper, HttpRequestConfigDO> {

    /**
     * 获取请求配置
     */
    public HttpRequestConfigDO getHttpRequestConfigFromPlatform(PlatformServiceEnum serviceConstant) throws DaoException {
        try {
            LambdaQueryWrapper<HttpRequestConfigDO> requestWrapper = Wrappers.<HttpRequestConfigDO>lambdaQuery()
                    .eq(HttpRequestConfigDO::getActive, ActiveEnum.ENABLE.getCode())
                    .eq(HttpRequestConfigDO::getPlatformCode, serviceConstant.getPlatformCode())
                    .eq(HttpRequestConfigDO::getServiceCode, serviceConstant.getServiceCode())
                    .orderByDesc(HttpRequestConfigDO::getId);
            List<HttpRequestConfigDO> httpRequestConfigList = this.list(requestWrapper);
            if (httpRequestConfigList.size() > 0) {
                return httpRequestConfigList.get(0);
            }
            return null;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    /**
     * 获取多条请求配置
     */
    public List<HttpRequestConfigDO> queryForList(String platformCode) throws DaoException {
        try {
            LambdaQueryWrapper<HttpRequestConfigDO> queryWrapper = Wrappers.<HttpRequestConfigDO>lambdaQuery()
                    .eq(HttpRequestConfigDO::getActive, ActiveEnum.ENABLE.getCode())
                    .eq(HttpRequestConfigDO::getPlatformCode, platformCode);
            List<HttpRequestConfigDO> requestConfigList = this.list(queryWrapper);
            if (requestConfigList == null) {
                return new ArrayList<>();
            }
            return requestConfigList;
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    /**
     * 根据service_code获取请求配置
     */
    public HttpRequestConfigDO queryForObject(PlatformServiceEnum serviceConstant) throws DaoException {
        try {
            LambdaQueryWrapper<HttpRequestConfigDO> queryWrapper = Wrappers.<HttpRequestConfigDO>lambdaQuery()
                    .eq(HttpRequestConfigDO::getActive, ActiveEnum.ENABLE.getCode())
                    .eq(HttpRequestConfigDO::getPlatformCode, serviceConstant.getPlatformCode())
                    .eq(HttpRequestConfigDO::getServiceCode, serviceConstant.getServiceCode());
            List<HttpRequestConfigDO> requestConfigList = this.list(queryWrapper);
            return requestConfigList.parallelStream()
                    .findFirst()
                    .orElse(new HttpRequestConfigDO());
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

    /**
     * 逻辑删除 网络服务请求配置
     */
    public void deleteHttpRequestConfig(PlatformServiceEnum serviceConstant) throws DaoException {
        try {
            LambdaQueryWrapper<HttpRequestConfigDO> queryWrapper = Wrappers.<HttpRequestConfigDO>lambdaQuery()
                    .eq(HttpRequestConfigDO::getActive, ActiveEnum.ENABLE.getCode())
                    .eq(HttpRequestConfigDO::getPlatformCode, serviceConstant.getPlatformCode())
                    .eq(HttpRequestConfigDO::getServiceCode, serviceConstant.getServiceCode());
            List<HttpRequestConfigDO> httpRequestConfigList = this.list(queryWrapper);
            httpRequestConfigList.forEach(e -> e.setActive(ActiveEnum.DISABLE.getCode()));
            this.updateBatchById(httpRequestConfigList, BatchSizeEnum.UPDATE_SIZE_50.getSize());
        } catch (Exception e) {
            throw new DaoException(e);
        }
    }

}
